Skip to content

Add opt-in ELK layout support for mermaid diagrams#22

Merged
selimacerbas merged 1 commit into
selimacerbas:mainfrom
fuzzybear3:mermaid-elk-support
May 24, 2026
Merged

Add opt-in ELK layout support for mermaid diagrams#22
selimacerbas merged 1 commit into
selimacerbas:mainfrom
fuzzybear3:mermaid-elk-support

Conversation

@fuzzybear3
Copy link
Copy Markdown
Contributor

@fuzzybear3 fuzzybear3 commented May 15, 2026

Summary

Adds a mermaid_elk config option (default false). When enabled, the browser preview loads @mermaid-js/layout-elk@0.2.1 from jsDelivr and registers it before mermaid initializes, allowing diagrams to use:

%%{init: {"layout": "elk"}}%%

Why

ELK produces noticeably cleaner layouts for medium/large flowcharts and class diagrams than mermaid's default dagre layout. It's opt-in because it adds ~800 KB on top of what the preview already fetches from CDN, and most diagrams don't need it.

Changes

  • lua/markdown_preview/init.lua: new mermaid_elk config (default false); substitutes a __MERMAID_ELK__ placeholder when writing index.html, mirroring the existing __BOTTOM_PADDING__ pattern.
  • assets/index.html: adds a data-mermaid-elk attribute on <html>; in boot(), conditionally dynamic-imports the ELK layout module and calls mermaid.registerLayoutLoaders(...) before the first sync().

Test plan

  • With mermaid_elk = false (default), preview behaves exactly as before — no extra network request on boot.
  • With mermaid_elk = true, a diagram containing %%{init: {"layout": "elk"}}%% renders with the ELK layout.
  • If the ELK import fails, the existing boot try/catch logs the error and shows an inline error message rather than a blank preview.

Adds `mermaid_elk` config option (default false). When enabled, loads
@mermaid-js/layout-elk@0.2.1 from CDN and registers it before mermaid
initializes, enabling %%{init: {"layout": "elk"}}%% in diagrams.
@selimacerbas
Copy link
Copy Markdown
Owner

Thanks @fuzzybear3, this is the shape of opt-in I was hoping for. Default off, mirrors the bottom_padding placeholder pattern, fails gracefully inside the boot try/catch. Merging.

One thing for later (not blocking): if ELK fails to load we could show a one-time inline note in the preview header so the user knows it fell back to dagre. Open a separate issue if you want to track that.

@selimacerbas selimacerbas merged commit 4326ab6 into selimacerbas:main May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants